package com.yyp.car.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yyp.car.Iservice.IUserService;
import com.yyp.car.model.User;
import com.yyp.exception.CarException;

@Controller("userController")
@RequestMapping("user")
public class UserController {
	@Resource(name="userService")
	private IUserService userService;
	
	@RequestMapping("/changePassword")
	public String changePassword(String oldPassword,String newPassword,String conPassword,HttpSession session,RedirectAttributes attr){
		if(!newPassword.equals(conPassword)){
			throw new CarException("两次密码不一致");
		}
		User user=(User)session.getAttribute("user");
		String cPassword=userService.findPassword(user.getId());
		if(!oldPassword.equals(cPassword)){
			throw new CarException("原密码不正确");
		}
		userService.changePassword(user.getId(), newPassword);
		attr.addFlashAttribute("message", "密码修改成功,请重新登入");
		session.removeAttribute("user");
		return "redirect:/index/login";
	}
}
